package com.calm.cdemo.anim;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.Resources;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.calm.cdemo.R;
import com.calm.cdemo.base.BaseActivity;

/**
 * Create by Calm
 * 2020/3/9 15:07
 */
public class CircleMenu extends BaseActivity implements View.OnClickListener, View.OnTouchListener {
    private Button btnMenu,btnItem1,btnItem2,btnItem3,btnItem4,btnItem5;
    private boolean mIsOpen = false;
    @Override
    protected void init() {
        Log.e("CALM",CLASS);
        btnMenu = findViewById(R.id.circlemenu_btn_menu);
        btnItem1 = findViewById(R.id.circlemenu_btn_item1);
        btnItem2 = findViewById(R.id.circlemenu_btn_item2);
        btnItem3 = findViewById(R.id.circlemenu_btn_item3);
        btnItem4 = findViewById(R.id.circlemenu_btn_item4);
        btnItem5 = findViewById(R.id.circlemenu_btn_item5);
        btnMenu.setOnClickListener(this);
        btnItem1.setOnClickListener(this);
        btnItem2.setOnClickListener(this);
        btnItem3.setOnClickListener(this);
        btnItem4.setOnClickListener(this);
        btnItem5.setOnClickListener(this);
        btnItem1.setOnTouchListener(this);
        btnMenu.setOnTouchListener(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.circlemenu;
    }
    public int getStatusBarHeight(Context context) {
        Resources resources = context.getResources();
        int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
        int height = resources.getDimensionPixelSize(resourceId);
        return height;
    }
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        switch (view.getId()){
            case R.id.circlemenu_btn_menu:
                if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
                    Log.e("CALM","time:"+motionEvent.getEventTime());
                    Log.e("CALM",motionEvent.getX()+" "+motionEvent.getY());
                    Log.e("CALM",btnMenu.getX()+" "+btnMenu.getY()+" "+btnMenu.getWidth()+" "+btnMenu.getHeight());
                }else if(motionEvent.getAction() == MotionEvent.ACTION_UP){
                    Log.e("CALM","time:"+motionEvent.getEventTime());
                    Log.e("CALM",motionEvent.getX()+" "+motionEvent.getY());
                }
                break;
        }
        return false;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.circlemenu_btn_menu:
                if(mIsOpen){
                    mIsOpen = false;
                    doAnimClose(btnItem1,0,5,500);
                    doAnimClose(btnItem2,1,5,500);
                    doAnimClose(btnItem3,2,5,500);
                    doAnimClose(btnItem4,3,5,500);
                    doAnimClose(btnItem5,4,5,500);
                }else {
                    mIsOpen = true;
                    doAnimOpen(btnItem1,0,5,500);
                    doAnimOpen(btnItem2,1,5,500);
                    doAnimOpen(btnItem3,2,5,500);
                    doAnimOpen(btnItem4,3,5,500);
                    doAnimOpen(btnItem5,4,5,500);
                }
                break;
            case R.id.circlemenu_btn_item1:
                Toast.makeText(mActivity,"Click Item1",Toast.LENGTH_SHORT).show();
                break;
            case R.id.circlemenu_btn_item2:
                Toast.makeText(mActivity,"Click Item2",Toast.LENGTH_SHORT).show();
                break;
            case R.id.circlemenu_btn_item3:
                Toast.makeText(mActivity,"Click Item3",Toast.LENGTH_SHORT).show();
                break;
            case R.id.circlemenu_btn_item4:
                Toast.makeText(mActivity,"Click Item4",Toast.LENGTH_SHORT).show();
                break;
            case R.id.circlemenu_btn_item5:
                Toast.makeText(mActivity,"Click Item5",Toast.LENGTH_SHORT).show();
                break;
        }
    }
    private void doAnimOpen(View view,int index,int total,int radius){
        if(view.getVisibility() != View.VISIBLE){
            view.setVisibility(View.VISIBLE);
        }
        double degress = Math.toRadians(90)/(total - 1)*index;
        int translationX = -(int)(radius * Math.sin(degress));
        int translationY = -(int)(radius * Math.cos(degress));
        AnimatorSet set = new AnimatorSet();
        set.playTogether(
                ObjectAnimator.ofFloat(view,"translationX",0,translationX),
            ObjectAnimator.ofFloat(view,"translationY",0,translationY),
                ObjectAnimator.ofFloat(view,"scaleX",0f,1f),
                ObjectAnimator.ofFloat(view,"scaleY",0f,1f),
                ObjectAnimator.ofFloat(view,"alpha",0f,1f)
        );
        set.setDuration(500).start();
    }
    private void doAnimClose(View view,int index,int total,int radius){
        if(view.getVisibility() != View.VISIBLE){
            view.setVisibility(View.VISIBLE);
        }
        double degree = Math.PI * index / ((total - 1)*2);
        int translationX = -(int)(radius * Math.sin(degree));
        int translationY = -(int)(radius * Math.cos(degree));
        AnimatorSet set = new AnimatorSet();
        set.playTogether(
                ObjectAnimator.ofFloat(view,"translationX",translationX,0),
                ObjectAnimator.ofFloat(view,"translationY",translationY,0),
                ObjectAnimator.ofFloat(view,"scaleX",1f,0f),
                ObjectAnimator.ofFloat(view,"scaleY",1f,0f),
                ObjectAnimator.ofFloat(view,"alpha",1f,0f)
        );
        set.setDuration(500).start();
    }
}
